package read.niit.mock

import scala.collection.mutable.ArrayBuffer
import java.io.{File, PrintWriter}

import com.google.gson.Gson
import read.niit.bean.Reader

import scala.util.Random

object Simulator {

  //模拟数据

  val arr1 = ArrayBuffer[String]()
  for (i <- 1 to 333) {
    arr1 += "学生ID_20" + i
  }
  //年级ID
  val arr2 = Array("大一", "大二", "大三", "大四")
  //专业ID
  val arr3 = Array("哲学", "经济学", "法学", "教育学", "文学","历史学","理学", "工学", "农学", "医学", "管理学","艺术学")
  //阅读类别ID
  val arr4 = Array(
    "时事政治类", "文学小说类", "历史传记类","哲学宗教类", "旅游地理类", "经管类","法律类", "国防军事类", "艺术鉴赏类",
    "体育运动类","健康保健类","娱乐休闲类", "考试考证类", "科技类", "心理成长类","教育类", "医学类","航空航天类",
    "交通运输类","综合类"
  )
  //年级ID
  val arr5 = ArrayBuffer[String]()
  for (i <- 1 to 30) {
    arr5 += "日期_4." + i
  }



  //书ID与阅读类别、专业、年级、日期、时间的对应关系,
  val bookMap = collection.mutable.HashMap[String, ArrayBuffer[String]]()

  var bookID = 1
  for (gradeID <- arr2; majorID <- arr3; read_subjectID <- arr4; dateID <- arr5) {
    val key = new StringBuilder()
      .append(gradeID).append("^")
      .append(majorID).append("^")
      .append(dateID).append("^")
      .append(read_subjectID)

    val bookArr = ArrayBuffer[String]()
    for (i <- 1 to 10) {
      bookArr += "书ID_" + bookID
      bookID += 1
    }
    bookMap.put(key.toString(), bookArr)
  }


  def genBook() = {

    //随机年级ID
    val gradeIDRandom = arr2(Random.nextInt(arr2.length))
    //随机专业ID
    val majorIDRandom = arr3(Random.nextInt(arr3.length))
    //随机阅读类别ID
    val read_subjectIDRandom = arr4(Random.nextInt(arr4.length))
    val dateIDRandom = arr5(Random.nextInt(arr5.length))

    val key = new StringBuilder()
      .append(gradeIDRandom).append("^")
      .append(majorIDRandom).append("^")
      .append(dateIDRandom).append("^")
      .append(read_subjectIDRandom)

    //取出书
    val bookArr = bookMap(key.toString())
    //随机书ID
    val bookIDRandom = bookArr(Random.nextInt(bookArr.length))
    //随机
    val bookScoreRandom = Random.nextInt(11)+1
    //随机学生ID
    val studentID = arr1(Random.nextInt(arr1.length))


    Reader(dateIDRandom,studentID, majorIDRandom, gradeIDRandom, read_subjectIDRandom, bookIDRandom,bookScoreRandom)
  }

  //测试模拟数据
  def main(args: Array[String]): Unit = {
    val printWriter = new PrintWriter(new File("output/reader_info.json"))
    val gson = new Gson()
    for (i <- 1 to 18888) {
      println(s"第{$i}条")
      val jsonString = gson.toJson(genBook())
      println(jsonString)

      printWriter.write(jsonString + "\n")
      //Thread.sleep(200)
    }
    printWriter.flush()
    printWriter.close()
  }

}
